package com.yunchao.bank.configure.domain.model

import com.yunchao.bank.configure.data.model.ConfigureEntityModel

internal data class ConfigureModel(
    // 网络配置
    val serverIp: String,
    val serverPort: String = DEFAULT_SERVER_PORT,
    val icCardIp: String,
    val icCardPort: String = DEFAULT_IC_CARD_PORT,
    val pdaDeviceIp: String,
    val pdaDevicePort: String = DEFAULT_PDA_PORT,

    // 设备信息
    val personNumber: String = "3",
    val deviceNumber: String,
    val deviceId: String,

    val singlePower: Int = 10,

    val loopPower: Int = 30,

    // 仓储编号
    val stackCode: String = ""
) {
    init {
        // 仍然保留运行时检查
        require(singlePower in MIN_POWER..MAX_POWER) { "单扫功率越界" }
        require(loopPower in MIN_POWER..MAX_POWER) { "群扫功率越界" }
    }

    companion object {
        // 默认端口常量（根据图片中的端口号标注）
        const val DEFAULT_SERVER_PORT = "50002"
        const val DEFAULT_IC_CARD_PORT = "51317"
        const val DEFAULT_PDA_PORT = "52020"

        const val MIN_POWER = 0
        const val MAX_POWER = 30
    }

    /**
     * 验证所有网络配置是否有效
     */
    fun validateNetworkConfig(): Boolean {
        return serverIp.isNotBlank() && serverPort.isNotBlank() &&
                icCardIp.isNotBlank() && icCardPort.isNotBlank() &&
                pdaDeviceIp.isNotBlank() && pdaDevicePort.isNotBlank()
    }
}

internal val EMPTY_CONFIGURE_MODEL = ConfigureModel(
    serverIp = "",
    icCardIp = "",
    pdaDeviceIp = "",
    personNumber = "3",
    deviceNumber = "",
    deviceId = "",
    stackCode = "",
)

internal fun ConfigureModel.toEntityModel() = ConfigureEntityModel(
    serverIp = serverIp,
    serverPort = serverPort,
    icCardIp = icCardIp,
    icCardPort = icCardPort,
    pdaDeviceIp = pdaDeviceIp,
    pdaDevicePort = pdaDevicePort,
    personNumber = personNumber,
    deviceNumber = deviceNumber,
    deviceId = deviceId,
    singlePower = singlePower,
    loopPower = loopPower,
    stackCode = stackCode
)